//
//  HomeFoodViewController.m
//  VietQuan
//
//  Created by hainguyen on 11/12/11.
//  Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "HomeFoodViewController.h"
#import "ItemView.h"
#import "Item.h"
#import "ItemExtension.h"
#import "UIImageExtension.h"
#import "FoodDetailViewController.h"

@implementation HomeFoodViewController

@synthesize scrollView;
@synthesize itemsArray;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark -
#pragma mark Item View Controller Delegate

-(void) touchMe:(ItemView *)itemView{
	//NSLog(@"%@", itemViewController.titleLabel);
	FoodDetailViewController *controller = [[FoodDetailViewController alloc] init];
	[self.navigationController pushViewController:controller animated:YES];
	[controller getDetailFood:itemView.item];
	[controller release];
}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	[self.view addSubview:scrollView];	
	itemsArray = [[NSMutableArray alloc] initWithArray:[Item getItems]];
	CGFloat y = 00.00;
	CGFloat h = [itemsArray count] * 80 + 43;
	for (int i = 0; i < [itemsArray count]; i++) {
		ItemView *itemView = [[[NSBundle mainBundle] loadNibNamed:@"ItemView" owner:self options:nil] lastObject];
		Item *item = [itemsArray objectAtIndex:i];
		itemView.viewDelegate = self;
		[scrollView addSubview:itemView];
		[itemView setFrame:CGRectMake(0, y, 320, 80)];
		itemView.titleLabel.text = item.name;
		itemView.summaryLabel.text = item.excerpt;
		itemView.item = item;
		UIImage *image = [UIImage imageWithData:item.thumbnail];
		[image scaleToSize:CGSizeMake(96, 77)];
		UIImage *imageRoundCorner = [UIImage makeRoundCornersImage:image :15 :15];
		itemView.thumbnailImage.image = imageRoundCorner;
		y = y + 80.00;
	}
	[scrollView setContentSize:CGSizeMake(320, h)];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void) dealloc
{
	self.scrollView = nil;
	[itemsArray release];
	[super dealloc];
}

@end
